/**
 * Created with JetBrains PhpStorm.
 * User: khathirdstep1025
 * Date: 9/27/12
 * Time: 11:22 PM
 * To change this template use File | Settings | File Templates.
 */
$(document).ready(function(){

    var link = "http://localhost/webbanhang/index.php/"; // Link to actual host -> call controller

    $("ul.products form").submit(function() {

        $.get(link + "cart/show_total_content_item", function(count){
            //alert(count);
            if (count == 8){
                alert("Can't add more! Limit to 8 products per order!");
                return false;
            }

        });

        var id = $(this).find('input[name=product_id]').val();
        var qty = $(this).find('input[name=quantity]').val();

        $.get(link + "cart/get_store_qty/" + id, function(status){
           // alert(id);
            if (status == 'het_hang'){
                alert("No more quantity!");
            } else{
                $.post(link + "cart/add_cart_item", { product_id: id, quantity: qty, ajax: '1' },
                    function(data){
                        if (data == 'true'){
                            $.get(link + "cart/show_cart", function(cart){ // Get the contents of the url cart/show_cart
                                $("#cart_content").html(cart); // Replace the information in the div #cart_content with the retrieved data
                            });
                            $.get(link + "cart/show_total_cart", function(total){
                                $(".shopping-cart-info").html(
                                    total + ' product(s)');
                            });
                        }else{
                            alert(data);
                        }
                    });
            }

        });

        return false;
    });

    $(".empty").live("click", function(){
        $.get(link + "cart/empty_cart", function(){
            $.get(link + "cart/show_cart", function(cart){
                $("#cart_content").html(cart);
            });
            $.get(link + "cart/show_total_cart", function(total){
                $(".shopping-cart-info").html(
                    total + ' product(s)');
            });
        });

        return false;
    });

});
